<!-- 导入信息复审 -->
<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="visible"
    width="600px"
    @close="onClose"
  >
    <div class="import-container">
      <!-- 下载模板 -->
      <div class="upload-item">
        <span class="label">复审资料模板</span>
        <el-button size="small" @click="downloadTemplate"
          >下载复审资料模板</el-button
        >
      </div>

      <!-- 上传文件 -->
      <div class="upload-item">
        <span class="label">上传复审资料</span>
        <el-upload
          ref="upload"
          :action="uploadFileUrl"
          :auto-upload="false"
          :before-upload="handleBeforeUpload"
          :file-list="fileList"
          :on-change="handleChange"
          :on-remove="handleRemove"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :headers="headers"
          accept=".xlsx,.xls"
          drag
          ><i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或点击上传</div>
          <div class="el-upload__tip" slot="tip">
            请上传大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>，
            格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> 的文件
          </div>
        </el-upload>
      </div>

      <!-- 提示信息 -->
      <div v-if="uploadStatus" class="upload-status">
        {{ uploadStatus }}
        <el-link type="danger" style="margin-left: 10px" @click="removeFile"
          >删除</el-link
        >
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">导入</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
  name: "ImportReexaminationDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dialogTitle: "导入复审资料",
      fileList: [],
      uploadStatus: "",
      uploadedFile: null,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传文件服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileSize: 10, // 文件大小限制(MB)
      fileType: ["xlsx", "xls"], // 允许上传的文件类型
    };
  },
  methods: {
    // 下载模板
    downloadTemplate() {
      const baseUrl = process.env.VUE_APP_DOWNLOAD_BASE_URL;
      const filePath = "/statics/2025/10/30/Excel.xlsx";
      const fullUrl = `${baseUrl}${filePath}`;

      this.$download.saveAs(fullUrl, "证书信息模板.xlsx");
    },

    // 文件改变事件
    handleChange(file, fileList) {
      this.fileList = fileList;
      this.uploadedFile = file.raw;
    },

    // 删除文件
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.uploadedFile = null;
    },

    // 上传成功
    handleSuccess(response, file, fileList) {
      this.uploadedFile = file;
      this.uploadStatus = `${file.name} 100%上传成功`;
    },

    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split(".");
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(
            `文件格式不正确，请上传${this.fileType.join("/")}格式文件!`
          );
          return false;
        }
      }
      // 校检文件名是否包含特殊字符
      if (file.name.includes(",")) {
        this.$modal.msgError("文件名不正确，不能包含英文逗号!");
        return false;
      }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      this.$modal.loading("正在上传文件，请稍候...");
      this.number++;
      return true;
    },

    // 超过限制
    onExceed() {
      this.$message.warning("最多只能上传一个文件！");
    },
    // 上传成功处理
    handleUploadSuccess(res, file) {
      this.$modal.closeLoading();
      if (res.code === 200) {
        this.$message.success("导入成功");
        this.$emit("import-success", {
          file: this.uploadedFile,
          mode: this.uploadMode,
        });
        this.cancel();
      } else {
        this.$modal.msgError(res.msg || "导入失败");
      }
      this.uploadLoading = false;
    }, // 上传失败处理
    handleUploadError(err) {
      this.$modal.closeLoading();
      this.$modal.msgError("上传文件失败，请重试");
      this.uploadLoading = false;
    },
    // 删除已上传文件
    removeFile() {
      this.$refs.upload.clearFiles();
      this.uploadedFile = null;
      this.uploadStatus = "";
    },

    // 取消
    cancel() {
      this.$emit("update:visible", false);
      this.reset();
    },

    // 提交导入
    submit() {
      if (!this.uploadedFile) {
        this.$message.warning("请先上传文件！");
        return;
      }

      // 模拟提交逻辑
      this.$message.success("导入成功！");
      this.$emit("import-success", { fileName: this.uploadedFile.name });
      this.cancel();
    },

    // 关闭对话框时重置
    onClose() {
      this.reset();
    },

    // 重置状态
    reset() {
      this.fileList = [];
      this.uploadedFile = null;
      this.uploadStatus = "";
    },
  },
};
</script>

<style scoped>
.import-container {
  padding: 20px;
}

.upload-item {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.label {
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.upload-status {
  margin-top: 10px;
  color: #67c23a;
  font-size: 14px;
}
</style>
